package com.zzy.util;
/*
在实际开发中，url和对应的访问权限可能需要从数据库中读取，我们可以定义一个工具类从数据库中读取访问权限并传递给Shiro。
*/

import org.apache.shiro.config.Ini;
import org.apache.shiro.util.CollectionUtils;
import org.apache.shiro.web.config.IniFilterChainResolverFactory;
import org.springframework.beans.factory.FactoryBean;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

public class MyChainDefinitions implements FactoryBean<Ini.Section> {
    public static final String PREMISSION_STRING = "perms[{0}]";
    public static final String ROLE_STRING = "roles[{0}]";
    private String filterChainDefinitions;

    public void setFilterChainDefinitions(String filterChainDefinitions) {
        this.filterChainDefinitions = filterChainDefinitions;
    }


    public Ini.Section getObject() throws Exception {
        //urls可以从数据库查出来，此处省略代码，直接模拟几条数据
        Set<String> urls = new LinkedHashSet();
        urls.add("/dotest1.html");// urls 和 permsMap 一一对应
        urls.add("/doget2.html");
        urls.add("/test2.html");
        //每个url对应的权限也可以从数据库中查出来，这里模拟几条数据
        Map<String, String[]> permsMap = new HashMap();
        permsMap.put("/dotest1.html", new String[]{"perm1", "admin"});
        permsMap.put("/doget2.html", new String[]{"perm1"});//访问 doget2.html需要 perm1 角色的权限
        permsMap.put("/test2.html", new String[]{"role1"});//访问 test2.html 需要 role1 角色的权限
        //加载配置默认的过滤链
        Ini ini = new Ini();
        ini.load(filterChainDefinitions);
        Ini.Section section = ini.getSection(IniFilterChainResolverFactory.URLS);
        if (CollectionUtils.isEmpty(section)) {
            section = ini.getSection(Ini.DEFAULT_SECTION_NAME);
        }
        for (String url : urls) {
            System.out.println(url);
            String[] perms = permsMap.get(url);
            System.out.println(url+":----:"+perms);
            StringBuilder permFilters = new StringBuilder();
            for (int i = 0; i < perms.length; i++) {
                permFilters.append(perms[i]).append(",");
            }
            //去掉末尾的逗号
            String str = permFilters.substring(0, permFilters.length() - 1);
            //生成结果如：/dotest1.html = authc, perms[admin]
            section.put(url, MessageFormat.format(PREMISSION_STRING, str));
        }
        return section;
    }

    public Class<?> getObjectType() {
        return this.getClass();
    }

    public boolean isSingleton() {
        return false;
    }
}
